package com.yuxinmuyu.sports_management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuxinmuyu.sports_management.bean.entiy.SNews;
import com.yuxinmuyu.sports_management.bean.request.PageRequest;
import com.yuxinmuyu.sports_management.exception.BusinessException;
import com.yuxinmuyu.sports_management.mapper.SNewsMapper;
import com.yuxinmuyu.sports_management.service.SNewsService;
import com.yuxinmuyu.sports_management.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author 张钦栩
 * @Email 1784746527@qq.com
 * @Create 2024-06-29
 * @Description
 */
@Service
public class SNewsServiceImpl implements SNewsService {
    @Resource
    private SNewsMapper sNewsMapper;

    @Override
    public Page<SNews> getPage(PageRequest request) {
        return sNewsMapper.selectPage(new Page<>(request.getCurrentPage(), request.getPageSize()),null);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SNews request) {
        request.setAuthor(UserUtil.getUserName());
        request.setOperator(UserUtil.getUserName());
        request.setLastDate(new Date());
        request.setPublishTime(new Date());
        int n=sNewsMapper.insert(request);
        if(n==0){
            throw new BusinessException("插入失败！");
        }
    }

    @Override
    public void delete(SNews request) {
        int n=sNewsMapper.deleteById(request.getNewID());
        if(n==0){
            throw new BusinessException("删除失败！");
        }
    }
}
